﻿/*{
author=pk2.2（风铃等）
version=2.0
date=2023/10/12
}*/

/***
AI调度兵器吃紧,或者兵器富裕的时候可以通过买卖兵装来平衡局面
***/

// ## 2023/05/03 # 铃 # 修复bug,美化显示。 ##
// ## 2023/05/02 # 铃 # 适配新的内政系统。 ##
// ## 2023/05/01 # 铃 # 完善了新的参数。 ##
// ## 2022/12/03 # 铃 # 新做了AI的买卖兵装功能。 ##

namespace AI买卖兵装
{
	// ================ CUSTOMIZE ================
	const bool 调试模式 = false;
	// ===========================================

	class Main
	{

		Main()
		{
			pk::bind(202, pk::trigger202_t(onAIRunningOrder));
		}

		void onAIRunningOrder(pk::ai_context @context, pk::building @building, int cmd)
		{
			if (cmd == 据点AI_买卖兵装)
			{
				int force_id;
				int pop_gain_final;
				pk::force@ force = context.force;

				int 兵装_枪 = pk::get_weapon_amount(building, 兵器_枪);
				int 兵装_戟 = pk::get_weapon_amount(building, 兵器_戟);
				int 兵装_弩 = pk::get_weapon_amount(building, 兵器_弩);
				int 兵装_马 = pk::get_weapon_amount(building, 兵器_战马);

				int 总兵装 = 兵装_枪 + 兵装_戟 + 兵装_弩 + 兵装_马;
				int 总兵器 = 0;

				bool 特色_枪 = false;
				bool 特色_戟 = false;
				bool 特色_弩 = false;
				bool is_city = building.get_id() < 城市_末;

				if (is_city)
				{
					pk::city @city = pk::building_to_city(building);
					特色_枪 = city.tokusan[pk::equipment_id_to_heishu(兵器_枪)];
					特色_戟 = city.tokusan[pk::equipment_id_to_heishu(兵器_戟)];
					特色_弩 = city.tokusan[pk::equipment_id_to_heishu(兵器_弩)];
				}
				int building_gold = pk::get_gold(building);
				int building_food = pk::get_food(building);
				int building_troops = ai::marchable_troops[building.get_id()];

				bool 资金紧张 = building_gold < 1000;
				bool 资源紧张 = building_gold * 8 + building_food < 50000;
				bool 资源见底 = building_gold * 8 + building_food < 20000;
				bool 资金不多 = building_gold < 15000;

				bool 总兵装过剩 = 总兵装 > building_troops * 2;
				bool 总兵装足够 = 总兵装 > building_troops * 1;

				bool 战争状态 = pk::enemies_around(building);

				// 兵装列表
				array<array<int>> weapon_list = {
					{兵器_枪, 兵装_枪},
					{兵器_戟, 兵装_戟},
					{兵器_弩, 兵装_弩},
					{兵器_战马, 兵装_马}
				};

				weapon_list.sort(function(a, b) {
					return a[1] > b[1];
				});

				// 兵器列表
				array<array<int>> punch_list = {
					{兵器_冲车, 0},
					{兵器_井阑, 0},
					{兵器_投石, 0},
					{兵器_木兽, 0},
					{兵器_斗舰, 0},
					{兵器_楼船, 0} };

				for (int i = 0; i <= 5; ++i)
				{
					int amount = pk::get_weapon_amount(building, punch_list[i][0]);
					punch_list[i][1] = amount;
					总兵器 += amount;
				}

				punch_list.sort(function(a, b) {
					return a[1] > b[1];
				});

				string building_name = pk::decode(pk::get_name(building));
				// pk::trace(pk::format("building_nam:{},粮草不足:{},盈余不足 :{},士兵偏多:{},战争状态:{}", building_name,粮草不足,盈余不足 ,士兵偏多,战争状态));

				int weapon_dif_final = 0;
				int punch_dif_final = 0;
				int boat_dif_final = 0;

				int weapon_add_final = 0;

				int gold_add = 0;
				int person_count = pk::get_person_list(building, pk::mibun_flags(身份_君主, 身份_都督, 身份_太守, 身份_一般)).count;

				// 实在是太穷的时候
				if (资源见底 and building.troops > 10000 and 总兵装 > 30000 and weapon_list[0][1] > 15000)
				{
					weapon_dif_final = weapon_list[0][1] / 2;
					gold_add = int(weapon_dif_final * 0.2);
					pk::add_weapon_amount(building, weapon_list[0][0], -weapon_dif_final, true);
					pk::add_gold(building, gold_add, true);
					// pk::history_log(building.pos, pk::get_force(building.get_force_id()).color, pk::encode(pk::format("\x1b[1x{}资金紧张,卖出{}{}用于周转,获得资金{}。", pk::decode(pk::get_name(building)), 兵装名字(weapon_list[0][0]), int(weapon_dif_final), gold_add)));
					if (weapon_dif_final > 25000)
						pk::history_log(building.pos, force.color, pk::encode(pk::format("\x1b[1x{}\x1b[0x资源见底,向商人卖出一批\x1b[1x{}\x1b[0x用于周转。", building_name, ch::get_weapon_name2(weapon_list[0][0]))));
					if (调试模式)
						pk::trace(pk::format("building_nam:{}卖兵装,资金紧张:{},总兵装足够{},weapon_dif_final:{},gold_add:{},战争状态:{}", building_name, 资金紧张, 总兵装, weapon_dif_final, gold_add, 战争状态));

					return;
				}

				// 资金严重短缺的时候,卖出兵装进行周转,总量大于3万才有周转的意义
				if (((资金紧张 or 资源紧张) and 总兵装足够 and 总兵装 > 50000 and weapon_list[0][1] > 30000 and weapon_list[0][1] > int(building.troops * 0.6)) and !战争状态)
				{
					weapon_dif_final = pk::min(30000, weapon_list[0][1] / 2) + pk::rand(10000);
					gold_add = int(weapon_dif_final * 0.2);
					pk::add_weapon_amount(building, weapon_list[0][0], -weapon_dif_final, true);
					pk::add_gold(building, gold_add, true);
					// pk::history_log(building.pos, pk::get_force(building.get_force_id()).color, pk::encode(pk::format("\x1b[1x{}资金紧张,卖出{}{}用于周转,获得资金{}。", pk::decode(pk::get_name(building)), 兵装名字(weapon_list[0][0]), int(weapon_dif_final), gold_add)));
					if (weapon_dif_final > 25000)
						pk::history_log(building.pos, force.color, pk::encode(pk::format("\x1b[1x{}\x1b[0x资金紧张,向商人卖出一批\x1b[1x{}\x1b[0x用于周转。", building_name, ch::get_weapon_name2(weapon_list[0][0]))));
					if (调试模式)
						pk::trace(pk::format("building_nam:{}卖兵装,资金紧张:{},总兵装足够{},weapon_dif_final:{},gold_add:{},战争状态:{}", building_name, 资金紧张, 总兵装, weapon_dif_final, gold_add, 战争状态));

					return;
				}

				// 兵装太过剩的时候,卖出兵装换取资金,总量大于12万才有卖出的意义
				if ((资金不多 and 总兵装过剩 and 总兵装 > 100000 and weapon_list[0][1] > building.troops and weapon_list[0][1] >= (is_city ? 80000 : 50000)) and !战争状态)
				{
					weapon_dif_final = weapon_list[0][1] / 2 + pk::rand(10000);
					gold_add = int(weapon_dif_final * 0.2);
					pk::add_weapon_amount(building, weapon_list[0][0], -weapon_dif_final, true);
					pk::add_gold(building, gold_add, true);
					if (weapon_dif_final > 25000)
						pk::history_log(building.pos, force.color, pk::encode(pk::format("\x1b[1x{}\x1b[0x向商人卖出过剩的\x1b[1x{}\x1b[0x用于周转。", building_name, ch::get_weapon_name2(weapon_list[0][0]))));
					return;
				}

				// 资金严重短缺的时候,卖出兵器进行周转,总量大于5个才有周转的意义
				if ((资金紧张 and 总兵器 > 10 and 总兵器 > building.troops / 10000 and punch_list[0][1] > 5) and !战争状态)
				{
					punch_dif_final = punch_list[0][1] / 2 + pk::rand(2);
					gold_add = punch_dif_final * ((punch_list[0][0] < 兵器_斗舰) ? 1500 : 700);
					pk::add_weapon_amount(building, punch_list[0][0], -punch_dif_final, true);
					pk::add_gold(building, gold_add, true);
					if (punch_dif_final >= 5)
						pk::history_log(building.pos, force.color, pk::encode(pk::format("\x1b[1x{}\x1b[0x资金紧张,向商人卖出部分\x1b[1x{}\x1b[0x用于周转。", building_name, ch::get_weapon_name2(punch_list[0][0]))));
					return;
				}

				// 兵装太过剩的时候,卖出兵器换取资金,总量大于12个才有卖出的意义
				if ((!资金紧张 and 资金紧张 and 总兵器 > 15 and 总兵器 > building.troops / 3000 and punch_list[0][1] > 10) and !战争状态)
				{
					punch_dif_final = punch_list[0][1] / 2 + pk::rand(2);
					gold_add = punch_dif_final * ((punch_list[0][0] < 兵器_斗舰) ? 1500 : 700);
					pk::add_weapon_amount(building, punch_list[0][0], -punch_dif_final, true);
					pk::add_gold(building, gold_add, true);
					if (punch_dif_final >= 5)
						pk::history_log(building.pos, pk::get_force(building.get_force_id()).color, pk::encode(pk::format("\x1b[1x{}\x1b[0x向商人过剩的\x1b[1x{}\x1b[0x。", building_name, ch::get_weapon_name2(punch_list[0][0]))));
					return;
				}

				// 买入兵装
				for (int i = 0; i <= 3; ++i)
				{
					if (ch::neibor_enemy_base_count(building, 1, false) > 1 and person_count > 2 and building.troops > 10000 and pk::get_gold(building) > 5000 and pk::get_weapon_amount(building, 1) + pk::get_weapon_amount(building, 2) + pk::get_weapon_amount(building, 3) + pk::get_weapon_amount(building, 4) < building.troops and weapon_list[i][1] < 10000)
					{
						weapon_add_final = 15000 + pk::rand(10000);
						if (is_city)
							weapon_add_final /= 2;
						gold_add = weapon_add_final / 5;
						pk::add_weapon_amount(building, weapon_list[i][0], weapon_add_final, true);
						pk::add_gold(building, -gold_add, true);
						if (weapon_add_final > 20000)
							pk::history_log(building.pos, pk::get_force(building.get_force_id()).color, pk::encode(pk::format("\x1b[1x{}\x1b[0x前线\x1b[1x{}\x1b[0x短缺,向商人买入一些补充武库。", building_name, ch::get_weapon_name2(weapon_list[i][0]))));
						return;
					}
				}
			}
		}

	} // class

	Main main;
}